#include "inc/mainwindow.h"
#include "ui_mainwindow.h"
#include "inc/zg291.h"
#include <QMessageBox>
#include <QTimer>
#include <QDialog>
#include <QInputDialog>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , serialPort(new QSerialPort(this))
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_connect_triggered()
{
    COMconfig *comconfig = new COMconfig(serialPort, this);
    comconfig->setWindowModality(Qt::ApplicationModal); // 设置为应用程序模态
    comconfig->setWindowFlags(Qt::Window); // 设置为独立窗口
    connect(comconfig, &COMconfig::portConnect, comconfig, &COMconfig::close);
    connect(comconfig, &COMconfig::destroyed, comconfig, &QObject::deleteLater);
    connect(comconfig, &COMconfig::portConnected, this, &MainWindow::handleDeviceSwitch);
    comconfig->show();

}
void MainWindow::handleDeviceSwitch(const QString &deviceType) {
    this->deviceType = deviceType;
    QWidget *currentWidget = ui->stackedWidget->currentWidget();
        if (currentWidget) {
            ui->stackedWidget->removeWidget(currentWidget);
            currentWidget->deleteLater();
        }
        QString portInfo;
                if (serialPort && serialPort->isOpen()) {
                    // 获取串口参数
                    QString portName = serialPort->portName();
                    QString baudRate = QString::number(serialPort->baudRate());
                    QString dataBits;
                    switch(serialPort->dataBits()) {
                        case QSerialPort::Data5: dataBits = "5"; break;
                        case QSerialPort::Data6: dataBits = "6"; break;
                        case QSerialPort::Data7: dataBits = "7"; break;
                        case QSerialPort::Data8: dataBits = "8"; break;
                        default: dataBits = "N/A"; break;
                    }
                    QString stopBits;
                    switch(serialPort->stopBits()) {
                        case QSerialPort::OneStop: stopBits = "1"; break;
                        case QSerialPort::OneAndHalfStop: stopBits = "1.5"; break;
                        case QSerialPort::TwoStop: stopBits = "2"; break;
                        default: stopBits = "N/A"; break;
                    }
                    QString parity;
                    switch(serialPort->parity()) {
                        case QSerialPort::NoParity: parity = "N"; break;
                        case QSerialPort::EvenParity: parity = "E"; break;
                        case QSerialPort::OddParity: parity = "O"; break;
                        case QSerialPort::SpaceParity: parity = "S"; break;
                        case QSerialPort::MarkParity: parity = "M"; break;
                        default: parity = "N/A"; break;
                    }

                    // 格式化连接信息
                    portInfo = QString("%1 %2 %3 %4 %5")
                              .arg(portName)
                              .arg(baudRate)
                              .arg(dataBits)
                              .arg(stopBits)
                              .arg(parity);
                } else {
                    portInfo = "未连接";
                }

                // 假设有一个名为serson_online的QLineEdit或QLabel控件
                // 如果是QLineEdit:
                ui->serson_online->setText(portInfo);
        // 根据设备类型创建新界面
        if (deviceType == "ZG291") {
            zg291Widget = new ZG291(serialPort,this);
            ui->stackedWidget->addWidget(zg291Widget);
            ui->stackedWidget->setCurrentWidget(zg291Widget);

        }
}

void MainWindow::on_action_triggered()
{
//     if (!serialPort || !serialPort->isOpen()) {
//         QMessageBox::warning(this, "警告", "未检测到设备连接，请先连接设备！");
//          return;
//       }
//     bool ok;
//             QString password = QInputDialog::getText(this, "管理员验证",
//                                                   "请输入管理员密码:",
//                                                   QLineEdit::Password,
//                                                   "", &ok);

//             if (!ok) {
//                 return; // 用户取消了输入
//             }

//             if (password != "admin123") {
//                 QMessageBox::warning(this, "错误", "密码错误！");
//                 return;
//             }
//     if(deviceType=="ZG291")
//     {
//         Administrator *admin =new Administrator(serialPort);
//         admin->setWindowModality(Qt::ApplicationModal);
//         admin->setWindowFlags(Qt::Window);
//         connect(admin, &Administrator::admin_config_succeed, admin, &COMconfig::close);
//         connect(admin, &Administrator::destroyed, admin, &QObject::deleteLater);
//         admin->show();
//     }
}

void MainWindow::on_network_triggered()
{
    if (!serialPort || !serialPort->isOpen()) {
        QMessageBox::warning(this, "警告", "未检测到设备连接，请先连接设备！");
         return;
      }
    internet *net=new internet(serialPort);
    net->setWindowModality(Qt::ApplicationModal);
    net->setWindowFlags(Qt::Window);
    connect(net, &internet::netportConnect, net, &COMconfig::close);
    connect(net, &internet::destroyed, net, &QObject::deleteLater);
    net->show();
}
